package com.juyu.miliao.scheduler.service;

import java.util.List;

import com.juyu.miliao.common.netease.NetEaseCloudUserService;
import com.juyu.miliao.scheduler.dao.mysql.UserInfoDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.juyu.miliao.scheduler.dao.mysql.SuspendDao;
import com.juyu.miliao.scheduler.dao.mysql.UserLoginDao;

/**
 * @author pengs
 * 账号解封定时任务
 */
@Component
@Slf4j
public class SuspendService {

	@Autowired
	private SuspendDao suspendDao;
	@Autowired
	private UserLoginDao userLoginDao;
	@Autowired
	private UserInfoDao userInfoDao;
	//每天01点执行一次
	@Scheduled(cron = "0 0 1 * * ?")
	public void userSuspendQuartz() {
		log.info("解封账号定时任务执行");
		//查询  需要解封集合
		List<Integer> userIds = suspendDao.getSuspends();
		//更新账户状态
		if (userIds.size()>0) {
			userLoginDao.updateAccountEnable(userIds);
			userInfoDao.updateUserEnable(userIds);
			//更新查封表数据状态
			suspendDao.updateSuspendState(userIds);
			userIds.forEach(a->{
				try {
					String temp = NetEaseCloudUserService.unBlockUserById(a);
					if (!temp.substring(temp.indexOf("code") + 6, temp.indexOf("code") + 9).equals("200")) {
						log.error("解封云信账号异常：userId={}", a);
					}
				} catch (Exception e) {
					log.error("解封云信账号异常：userId={}", a, e);
				}
			});
		}
		log.info("解封账号定时任务结束");
	}
}
